﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Text.RegularExpressions;

namespace TrackerRealm.Jazz.Common
{
    /// <summary>
    /// 
    /// </summary>
    public static class StringExtensionMethods
    {

        #region IsNullOrEmpty
        /// <summary>
        /// Returns true if this 'string' is a null reference or an empty string (""); otherwise, false.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return String.IsNullOrEmpty(s);
        }
        #endregion
        #region TryParseClassName
        /// <summary>
        /// Parses a string of the format "className.memberName" into
        /// two strings.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="className"></param>
        /// <param name="memberName"></param>
        /// <returns></returns>
        public static bool TryParseClassName(this string s, out string className, out string memberName)
        {
            className = "";
            memberName = "";
            if (s.IsNullOrEmpty())
                return false;
            string[] sArray = s.Split(new char[] { '.' });
            if (sArray.Length != 2)
                return false;
            className = sArray[0];
            memberName = sArray[1];
            return true;
        }
        #endregion
        #region TrimStart
        /// <summary>
        /// Removes the leading string specified by 'trimString' from the current String object.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="trimString"></param>
        /// <returns></returns>
        public static string Trimstart(this string s, string trimString)
        {
            if (s != null && s.StartsWith(trimString))
            {
                s = s.Remove(0, trimString.Length);
            }
            return s;
        }

        #endregion
        #region TrimEnd
        /// <summary>
        /// Removes the trailing string specified by 'trimString' from the current String object.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="trimString"></param>
        /// <returns></returns>
        public static string Trimend(this string s, string trimString)
        {
            if (s != null && s.EndsWith(trimString))
            {
                s = s.Remove(s.Length - trimString.Length, trimString.Length);
            }
            return s;
        }
        #endregion
        #region ToStringCamelSpaced
        /// <summary>
        /// Converts a "camel" string into a string with proper spacing.
        /// <para>
        /// For example the string 'MakeNewContact' will be converted to
        /// 'Make New Contact'
        /// </para>
        /// <para>
        /// Note that the extension methods '<see cref="TrimStart(string)"/>' and 
        /// '<see cref="TrimEnd(string)"/>' can be used to convert 'TjUnitTestType'
        /// to 'UnitTest'</para>
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToStringCamelSpaced(this string s)
        {
            // *** NOTE - The complexity of this method is primarily to
            // deal with the special cases.
            //            
            if (s == null) return "";
            // This is where the replacement is to start.
            // Typically don't replace the first character.
            int index = 0;
            Regex r = new Regex("[A-Z]");
            //
            // Special case: " lowercaseFollowedByUpperCase"
            // Determine if the string is of the format " lowercaseFollowedByUpperCase".
            // If that is the case then Replace operation should start at languageCode = 0.
            // This will result in the string " lowercase Followed By Upper Case".
            //
            // ^ beginning of string
            // [\s\t\v\f]*  zero of more whitespace characters
            // [a-z]+  one or more lower case characters
            // [A-Z]  the first upper case character has some lower case characters in front of it.
            Regex startIndex0 = new Regex(@"^[\s\t\v\f]*[a-z]+[A-Z]");
            Match m = startIndex0.Match(s);
            if (!m.Success)
            {
                // Special Case: Don't put a space at the begginning of the line.
                // Find the languageCode of the first upper case character to be used at
                // replacement starting point.
                // This will allow "UpperCase" to be coverted to "Upper Case"
                m = r.Match(s);
                if (m.Success) index = m.Index + 1;
                else return s;
            }
            // place a space in front of upper case characters starting
            // in the string 's' at languageCode+1
            return r.Replace(s, " $0", int.MaxValue, index + 1);
        }
        #endregion
    }
}
